//
//  HomeViewController.swift
//  test_swift
//
//  Created by fangyuan ding on 2022/2/23.
//

import UIKit

class HomeViewController: UIViewController {
    
    private lazy var dataArray : Array = {
        return ["OCToSwift","模式匹配","内存管理","访问控制","扩展","字符串和数组","泛型","协议","可选链","初始化","继承","下标","方法","属性","闭包","结构体和类","可选项","枚举","函数式编程","控制流","元组"]
        
    }()
    
    lazy var tableView:UITableView = {
        let tw = UITableView(frame:CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), style: .grouped)
        tw.backgroundColor = UIColor.init(red: 242/255.0, green: 242/255.0, blue: 242/255.0, alpha: 1.0)
        tw.delegate = self
        tw.dataSource = self
//        tw.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        return tw
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        edgesForExtendedLayout = .top
        view.addSubview(tableView)

      
    }
    
}
extension HomeViewController :UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath )
        //创建cell
          var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "cellId")
          
          if cell == nil {
              //自定义cell使用此方法
              cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cellId")

              //xib加载cell使用此方法
           // cell = Bundle.main.loadNibNamed("testCell", owner: nil, options: nil)?.last as? UITableViewCell
          }
      
        cell!.accessoryType = .disclosureIndicator
        cell!.selectionStyle = .default
        cell?.textLabel?.text = dataArray[indexPath.row]
        return cell!
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
      
        if indexPath.row == 0{
            self.navigationController?.pushViewController(OCToSwiftViewController(), animated: true)
        }else if indexPath.row == 1{
            self.navigationController?.pushViewController(PatternMatchingViewController(), animated: true)
        }else if indexPath.row == 2{
            self.navigationController?.pushViewController(MemoryManagementViewController(), animated: true)
        }else if indexPath.row == 3{
            self.navigationController?.pushViewController(AccessControlViewController(), animated: true)
        }else if indexPath.row == 4{
            self.navigationController?.pushViewController(ExtensionViewController(), animated: true)
        }else if indexPath.row == 5{
            self.navigationController?.pushViewController(StringAndArrayViewController(), animated: true)
        }else if indexPath.row == 6{
            self.navigationController?.pushViewController(GenericityViewController(), animated: true)
        }else if indexPath.row == 7{
            self.navigationController?.pushViewController(ProtocolViewController(), animated: true)
        }else if indexPath.row == 8{
            self.navigationController?.pushViewController(OptionalChainingViewController(), animated: true)
        }else if indexPath.row == 9{
            self.navigationController?.pushViewController(InitViewController(), animated: true)
        }else if indexPath.row == 10{
            self.navigationController?.pushViewController(InheritViewController(), animated: true)
        }else if indexPath.row == 11{
            self.navigationController?.pushViewController(IndexViewController(), animated: true)
        }else if indexPath.row == 12{
            self.navigationController?.pushViewController(MethodViewController(), animated: true)
        }else if indexPath.row == 13{
            self.navigationController?.pushViewController(PropertyViewController(), animated: true)
        }else if indexPath.row == 14{
            self.navigationController?.pushViewController(ClosureViewController(), animated: true)
        }else if indexPath.row == 15{
            self.navigationController?.pushViewController(StructAndClassViewController(), animated: true)
        }else if indexPath.row == 16{
            self.navigationController?.pushViewController(OptionalViewController(), animated: true)
        }else if indexPath.row == 17{
            self.navigationController?.pushViewController(EnumViewController(), animated: true)
        }else if indexPath.row == 18{
            self.navigationController?.pushViewController(FuncViewController(), animated: true)
        }else if indexPath.row == 19{
            self.navigationController?.pushViewController(ControlFlowViewController(), animated: true)
        }else if indexPath.row == 20{
            self.navigationController?.pushViewController(TupleViewController(), animated: true)
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
}
